﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CS2102_Music.Logic.LogicModels;

namespace CS2102_Music.Controllers
{
    public class TransactionController : Controller
    {
        //
        // GET: /Transaction/
        private Logic.Logic logic = Logic.Logic.GetInstance(new Logic.DbConnector());

        public ActionResult Index() //view cart
        {
            Logic.Cart.TransactionCart cartDetails = new Logic.Cart.TransactionCart();

            if (Session["Cart"] == null)
            {
                Session["Cart"] = cartDetails;
            }
            else
            {
                cartDetails = (Logic.Cart.TransactionCart)Session["Cart"];
            }

            ViewBag.cart = cartDetails;

            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;

            return View();
        }

        //just manipulating cart. Not yet to database as user have not checked out
        public string AddToCart(int type, int productID)
        {
            string added = "";
            bool existDB = false;
            bool existCart = false;
            UserL user = new UserL();

            if (Session["User"] == null)
                added = "Please log in!";
            else
            {
                user = (UserL)Session["User"];

                // check if user bought before (like iTunes help user check. 1 user 1 song)
                if (type == 1) //song
                {
                    existDB = logic.CheckUserBoughtSong(user.Email, productID);
                }
                else //album
                {
                    existDB = logic.CheckUserBoughtAlbum(user.Email, productID);
                }



                Logic.Cart.TransactionCart cart = new Logic.Cart.TransactionCart();
                // if user never buy before, check that is not in current cart
                if (!existDB)
                {            
                    if (Session["Cart"] == null)
                    {
                        existCart = false;                  
                        Session["Cart"] = cart;
                        cart.TotalCost = 0;
                    }
                    else
                    {
                        cart = (Logic.Cart.TransactionCart)Session["Cart"];

                        if (type == 1) //song
                        {
                            for (int i = 0; i < cart.Songs.Count(); i++)
                            {
                                if (cart.Songs[i].ID == productID)
                                {
                                    existCart = true;
                                    break;
                                }
                            }
                        }
                        else //album
                        {
                            for (int i = 0; i < cart.Albums.Count(); i++)
                            {
                                if (cart.Albums[i].ID == productID)
                                {
                                    existCart = true;
                                    break;
                                }
                            }
                        }
                    }
                }


                // add to CART if not exist in both database & current cart
                if (!existCart && !existDB)
                {
                    if (type == 1)
                    {
                        SongL song = new SongL();
                        song = logic.GetSongByID(productID);
                        cart.Songs.Add(song);
                        cart.TotalCost = Math.Round((cart.TotalCost + (song.Price)),2);

                    }
                    else
                    {
                        AlbumL album = new AlbumL();
                        album = logic.GetAlbumByID(productID);
                        cart.Albums.Add(album);
                        cart.TotalCost = Math.Round((cart.TotalCost + (album.Price)),2);
                    }
                }



                if (existDB)
                    added = "You bought this!";
                else
                    added = "Added/is in cart.";
            }

            return added;
        }
        public bool RemoveFromCart(int type, int productID)
        {
            bool remove = false;

            Logic.Cart.TransactionCart cart = new Logic.Cart.TransactionCart();
            if (Session["Cart"] != null)
            {
                cart = (Logic.Cart.TransactionCart)Session["Cart"];

                if (type == 1)
                {
                    SongL song = new SongL();
                    song = logic.GetSongByID(productID);
                    for (int k = 0; k < cart.Songs.Count(); k++)
                    {
                        if (productID == cart.Songs[k].ID)
                        {
                            cart.Songs.RemoveAt(k);
                            break;
                        }
                    }
                    cart.TotalCost = Math.Round((cart.TotalCost - (song.Price)),2);
                    remove = true;

                }
                else
                {
                    AlbumL album = new AlbumL();
                    album = logic.GetAlbumByID(productID);
                    for (int k = 0; k < cart.Albums.Count(); k++)
                    {
                        if (productID == cart.Albums[k].ID)
                        {
                            cart.Albums.RemoveAt(k);
                            break;
                        }
                    }
                    cart.TotalCost = Math.Round((cart.TotalCost - (album.Price)),2);
                    remove = true;
                }
            }

            return remove;
        }

        //check out
        public ActionResult CreateTransaction()
        {
            Logic.Cart.TransactionCart cart = new Logic.Cart.TransactionCart();
            UserL user = new UserL();
            DateTime currDateTime = DateTime.Now;

            if (Session["Cart"] != null)
            {
                if (Session["User"] != null)
                {
                    cart = (Logic.Cart.TransactionCart)Session["Cart"];
                    user = (UserL)Session["User"];

                    for (int i = 0; i < cart.Albums.Count; i++)
                    {
                        logic.BuyAlbum(user.Email, cart.Albums[i].ID, currDateTime, cart.Albums[i].Price);
                    }
                    for (int i = 0; i < cart.Songs.Count; i++)
                    {
                        string email = user.Email;
                        int song = cart.Songs[i].ID;
                        DateTime date = currDateTime;

                        logic.BuySong(user.Email, cart.Songs[i].ID, currDateTime, cart.Songs[i].Price);
                    }

                    logic.AutomaticEmailInvoice(user, cart);
                }

               
                Session["Cart"] = null;
            }


            return View();
        }


    }
}
